package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.History;
import com.example.flshtarvel.entity.Notes;
import com.example.flshtarvel.service.CollectService;
import com.example.flshtarvel.service.HistoryService;
import com.example.flshtarvel.service.LikeService;
import com.example.flshtarvel.service.NotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NotesController {
    @Autowired
    NotesService notesService;
    @Autowired
    LikeService likeService;
    @Autowired
    CollectService collectService;
    @Autowired
    HistoryService historyService;

    @PostMapping("/notes")
    public Map<String,Object> insertNotes(Notes notes, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        notes.setUserId((String)session.getAttribute("userId"));
        int result = notesService.insertNotes(notes);
        if(result<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
        }
        return map;
    }

    @GetMapping("/deleteNotes")
    public Map<String,Object> deleteNotes(@RequestParam("id")String notesId){
        Map<String,Object> map = new HashMap<>();
        int result = notesService.deleteNotes(notesId);
        if(result<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
        }
        return map;
    }

    @PostMapping("/editorNotes")
    public Map<String,Object> updateNotes(Notes notes){
        Map<String,Object> map = new HashMap<>();
        int result = notesService.updateNotes(notes);
        if(result<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
        }
        return map;
    }

    @GetMapping("/notes")
    public Map<String,Object> selectNotes(@RequestParam(name="page",defaultValue = "1") int page,
                                          @RequestParam(name="size",defaultValue = "10") int size){
        Map<String,Object> map = new HashMap<>();
        List<Notes> notesList = notesService.selectNotes(page,size);
        if(notesList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("notesList",notesList);
        }


        return map;
    }

    @GetMapping("/notes/{notesId}")
    public Map<String,Object> selectNotesByNotesId(@PathVariable("notesId")String notesId,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        Notes notes = notesService.selectNotesByNotesId(notesId);
        if(notes==null){
            map.put("msg",0);
        }else {
            //添加浏览记录
            History history = new History();
            history.setMessageId(notesId);
            history.setUserId((String)session.getAttribute("userId"));
            history.setArticleTypeId("04");
            history.setMessageName(notes.getTitle());
            historyService.history(history);


            map.put("msg",1);
            //是否点赞
            int isLike = likeService.selectLikeByMessageIdAndUserId(notesId,(String)session.getAttribute("userId"));
            //int isLike = routeLikeMapService.selectRouteLikeByRouteIdAndUserId(routeId,userId);
            //是否收藏
            int isCollect = collectService.selectCollectByMessageIdAndUserId(notesId,(String)session.getAttribute("userId"));
            //int isCollect = routeCollectMapService.selectRouteCollectByRouteIdAndUserId(routeId,userId);
            map.put("notes",notes);
            map.put("isLike",isLike);
            map.put("isCollect",isCollect);
        }

        return map;
    }

    @GetMapping("/selectNotesByUserId")
    public Map<String,Object> selectNotesByUserId(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        List<Notes> notesList = notesService.selectNotesByUserId((String)session.getAttribute("userId"));
        if(notesList.size()<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("notesList",notesList);
        }
        return map;
    }

    @GetMapping("/selectNotesById")
    public Map<String,Object> selectNotesById(@RequestParam("id")String id){
        Map<String,Object> map = new HashMap<>();
        Notes notes = notesService.selectNotesById(id);
        if(notes==null){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("notes",notes);
        }
        return map;
    }
}
